/***
	公共方法

***/
Date.prototype.format = function(fmt) {
	var o = {
		"M+" : this.getMonth()+1,                 //月份
		"d+" : this.getDate(),                    //日
		"h+" : this.getHours(),                   //小时
		"m+" : this.getMinutes(),                 //分
		"s+" : this.getSeconds(),                 //秒
		"q+" : Math.floor((this.getMonth()+3)/3), //季度
		"S"  : this.getMilliseconds()             //毫秒
	};
	if(/(y+)/.test(fmt)) {
		fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
	}
	for(var k in o) {
		if(new RegExp("("+ k +")").test(fmt)){
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
		}
	}
	return fmt;
}
//配置参数及方法
var yj = {
	//控制跳转   url格式:html/index.html"
	goPage:function(url){
		var str = location.pathname;
		var index = str.indexOf('html') + 4;
		var path  = str.substring(0,index);
		location.href =  location.protocol + '//' + location.host + path + url;
	},
	//取url参数
	getUrlParam:function(name){
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
	}
};
var user = JSON.parse(sessionStorage.getItem("backUserInfo"));
//转换时间
	function DateToUnix(string) {
                var f = string.split(' ', 2);
                var d = (f[0] ? f[0] : '').split('-', 3);
                var t = (f[1] ? f[1] : '').split(':', 3);
                return (new Date(
                        parseInt(d[0], 10) || null,
                        (parseInt(d[1], 10) || 1) - 1,
                        parseInt(d[2], 10) || null,
                        parseInt(t[0], 10) || null,
                        parseInt(t[1], 10) || null,
                        parseInt(t[2], 10) || null
                        )).getTime() / 1000;
            }
	function getMyDate(str){
		var oDate = new Date(str),
		oYear = oDate.getFullYear(),
		oMonth = oDate.getMonth()+1,
		oDay = oDate.getDate(),
		oHour = oDate.getHours(),
		oMin = oDate.getMinutes(),
		oSen = oDate.getSeconds(),
		oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最后拼接时间
		return oTime;
	};
	function getzf(num){
          if(parseInt(num) < 10){
              num = '0'+num;
          }
          return num;
    }
$(function () {
	//控制菜单跳转
	$(".sidebar-menu").on('click','li[data-href]',function(){
		var href = $(this).data("href");
		yj.goPage(href);
	});
	//获得登录信息
	if(user==null){
		window.location.href ='../../../login.html';
	}else{
		$(".skin-blue .main-header .navbar").append("<div class='userinfo'>欢迎您，<span>"+user.userName+"</span>，<a id='logout'>退出登录</a></div>")
	};
	//加载菜单
	$.ajax({
		url:'http://web.icecn.net/base/user/getUserResLimit.json',
		type:'POST',
		async:true,
		data:{
			userId: user.id
		},
		dataType: "json",
		xhrFields:{withCredentials:true},
		success: function(data){
			if(data.code === 200 ){
			  var a="";
			  var d=data.t;
			  if(d.length>0){
				  for(var i=0;i<d.length;i++){
					  if(d[i].children.length>0){
						  var b="";
						  for(var j=0;j<d[i].children.length;j++){
							if(d[i].children[j].isChoice==true){
							  b+="<li data-href='"+d[i].children[j].resUrl+"'><a><i class='fa fa-circle-o'></i>"+d[i].children[j].name+"</a></li>" 
							}else{
							  b+="<li data-href='"+d[i].children[j].resUrl+"' class='hide'><a><i class='fa fa-circle-o'></i>"+d[i].children[j].name+"</a></li>" 
							}
						  } 
						  a+="<li class='treeview'><a><i class='fa "+d[i].resKey+"'></i><span>"+d[i].name+"</span><span class='pull-right-container'> <i class='fa fa-angle-left pull-right'></i></span></a><ul class='treeview-menu'>"+b+"</ul></li>";
					  }
				  }
				  $(".sidebar-menu").html(a)
				  //选中菜单
				  $(".sidebar-menu li.treeview").eq($("body").attr("data-treeview")).addClass("active").find(".treeview-menu").children("li").eq($("body").attr("data-treeview-menu")).addClass("active");
			  }else{
				$("#modal-Permissions ul").html("<li>无数据</li>")
			  }
			}else{
			   alert(data.msg)	
			}
		}
   });
    //图片上传
	function fileupload(input,img,photoCover){
		var fileObj = document.getElementById(input).files[0];
		$.ajax({
			url:'http://39.106.145.173:8087/leveling/getToken.json',
			type:'POST',
			async:true,
			data:{
			},
			dataType: "json",
			success: function(data){
				var form = new FormData;
				form.append("file",fileObj);
				form.append("key",new Date().getTime()+".jpg");
				form.append("token",data.t.token);
				var domian=data.t.domain;
				$.ajax({
					url:'http://up.qiniu.com',
					type: 'POST',
					data:form,
					async: false,
					cache: false,
					contentType: false,
					processData: false,
					dataType: "json",
					success: function(data) {
					   $("."+img).html("<img src='"+domian+"/"+data.key+"'/>")
					   $("#"+photoCover).val(""+domian+"/"+data.key+"");
					},
				});
			}
	    });
	}
	
	$("#lefile1").change(
	  function(){
		 fileupload("lefile1","imgfile1","photoCover1")
	  }
	)
	$("#lefile2").change(
	  function(){
		 fileupload("lefile2","imgfile2","photoCover2")
	  }
	)
	$("#lefile3").change(
	  function(){
		 fileupload("lefile3","imgfile3","photoCover3")
	  }
	)
	//退出登录
	$("#logout").click(function(){
		$.ajax({
			url:'http://web.icecn.net/index/logout.json',
			type:'POST',
			async:true,
			data:{
			},
			dataType: "json",
			xhrFields:{withCredentials:true},
			success: function(data){
				if(data.code === 200 ){
				   sessionStorage.removeItem("backUserInfo");
				   window.location.href ='../../../login.html';
				}else{
					alert(data.msg)
				}
			}
	   });

	});
})
